package cl.pelu.operacion;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import cl.pelu.model.bo.dvo.ServicioBO;
import cl.pelu.model.bo.dvo.ServicioDVO;
import cl.pelu.model.bo.dvo.TablaDVO;
import cl.pelu.utils.PeluFacade;


public class MantenedorServiciosForm  extends javax.swing.JInternalFrame {

	private static MantenedorServiciosForm myInstance;
	
	
	private javax.swing.JLayeredPane mantenedorServicioLayeredPane;
	private javax.swing.JLayeredPane busquedaLayeredPane ;
	
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable serviciosTable;
	
	private javax.swing.JLabel nombreServicioLabel;
	private javax.swing.JLabel precioLabel;
	private javax.swing.JLabel tipoCabelloLabel;
	private javax.swing.JLabel busquedaServiciosLabel   ;
	private javax.swing.JLabel codigoLabel   ;
	
	private javax.swing.JTextField nombreServicioField;
	private javax.swing.JTextField precioField;
	private javax.swing.JComboBox tipoCabelloCombo;
	private javax.swing.JTextField busquedaServicioField;
	private javax.swing.JTextField codigoField;
	
	private javax.swing.JButton buscarButton;
	private javax.swing.JButton actualizarButton;
	private javax.swing.JButton nuevoButton;
	private javax.swing.JButton guardarButton;
	private javax.swing.JButton eliminarButton;
	
	
	
	private javax.swing.JCheckBox  tieneFormulaCheckBox ;
	
	
	private SimpleDateFormat df ;
	
	
	
	public MantenedorServiciosForm() {
		super("Mantenedor Servicio");
		initComponents();
	}

	private void initComponents() {
		
		

		jScrollPane1 = new javax.swing.JScrollPane();
		serviciosTable = new javax.swing.JTable();
		mantenedorServicioLayeredPane = new javax.swing.JLayeredPane();
		busquedaLayeredPane = new javax.swing.JLayeredPane();
		
		
		
		precioLabel = new javax.swing.JLabel("Precio");
		nombreServicioLabel = new javax.swing.JLabel("Nombre Servicio");
		tipoCabelloLabel    = new javax.swing.JLabel("Tipo Cabello");
		busquedaServiciosLabel    = new javax.swing.JLabel("Busqueda de Servicios");
		codigoLabel    = new javax.swing.JLabel("Codigo");
		
		tieneFormulaCheckBox = new javax.swing.JCheckBox  ("Tiene Formula");	
		
		precioField = new javax.swing.JTextField();
		nombreServicioField = new javax.swing.JTextField();
		codigoField = new javax.swing.JTextField();
		
		
		
		busquedaServicioField = new javax.swing.JTextField();
		buscarButton  = new javax.swing.JButton("Buscar");
		actualizarButton = new javax.swing.JButton("Actualizar");
		nuevoButton = new javax.swing.JButton("Nuevo");
		
		
		
		try {
			TablaDVO[] tablaDVO =  PeluFacade.getPeluqueriaComponent().getTipoCabello();
			tipoCabelloCombo = new javax.swing.JComboBox(tablaDVO);	
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
		}
		
		
		
		guardarButton = new javax.swing.JButton("Guardar");
		eliminarButton = new javax.swing.JButton("Eliminar");
		 
		
		
		busquedaServiciosLabel.setBounds(200, 1, 200, 20);
		
		codigoLabel 		.setBounds(30, 30, 200, 20);
		nombreServicioLabel .setBounds(30, 70, 200, 20);
		precioLabel			.setBounds(30, 110, 200, 20);
		tipoCabelloLabel	.setBounds(30, 150, 200, 20);
		codigoField			.setBounds(180, 30, 200, 20);
		nombreServicioField .setBounds(180, 70, 200, 20);
		precioField			.setBounds(180, 110, 200, 20);
		tipoCabelloCombo	.setBounds(180, 150, 200, 20);
		tieneFormulaCheckBox.setBounds(500, 30, 200, 20);
		busquedaServicioField.setBounds(400, 1, 300	, 20);
		guardarButton.setBounds(500,150 , 100, 20);
		eliminarButton.setBounds(850, 150, 100, 20);
		actualizarButton.setBounds(650,150 , 150, 20);
		buscarButton.setBounds(750,1 , 100, 20);
		nuevoButton.setBounds(900,1 , 100, 20);
		
		codigoField.setEditable(false);
		actualizarButton.setEnabled(false);
		eliminarButton.setEnabled(false);
		
		
		
		jScrollPane1.setViewportView(serviciosTable);
		mantenedorServicioLayeredPane.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(),"Detalle de Servicios"));
		mantenedorServicioLayeredPane.add(eliminarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(nombreServicioField,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(precioField,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(nombreServicioLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(precioLabel,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(guardarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(tipoCabelloLabel,	javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(tipoCabelloCombo,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(codigoLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(codigoField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(actualizarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		mantenedorServicioLayeredPane.add(tieneFormulaCheckBox,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
		
		
		busquedaLayeredPane.add(busquedaServiciosLabel,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(busquedaServicioField,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(buscarButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		busquedaLayeredPane.add(nuevoButton,javax.swing.JLayeredPane.DEFAULT_LAYER);
		
		
		
	
		
		setClosable(true);

		guardarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				
				limpiarTabla();
				guardarServicio();
				cargarTabla(new ServicioDVO());
			}
		});
		
		eliminarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
		
				limpiarTabla();
				eliminarServicio();
				cargarTabla(new ServicioDVO());
				
			}
		});
		
		

		
		nuevoButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {

				actualizarButton.setEnabled(false);
				eliminarButton.setEnabled(false);
				guardarButton.setEnabled(true);
			
				
				codigoField.setText("");
				nombreServicioField.setText("");
				precioField.setText("");
				tipoCabelloCombo.setSelectedIndex(0);
				tieneFormulaCheckBox.setSelected(false);
				
			}
		});

		
		actualizarButton .addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
			
				limpiarTabla();
				actualizarServicio();
				cargarTabla(new ServicioDVO());
				
			}
		});
		
		buscarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
			
				ServicioDVO servicio = new ServicioDVO();
				
				if(!busquedaServicioField.getText().isEmpty()){
					servicio.setNombre(busquedaServicioField.getText());
				}
				
				limpiarTabla();
				cargarTabla(servicio);
				
			}
		});

		
		
		
		serviciosTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				TableMouseClicked(evt);
			}
		});
		
		
		
		serviciosTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "codigo", "nombre", "precio", "tipo cabello","tiene formula" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false,false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		
			
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		
		
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(17, 17, 17)
						.addComponent(busquedaLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 50,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
						.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE, 200,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(27, 27, 27)
						.addComponent(mantenedorServicioLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 300,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(99, Short.MAX_VALUE)));
		
		
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(49, 49, 49)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(busquedaLayeredPane,javax.swing.GroupLayout.PREFERRED_SIZE, 1000,javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(mantenedorServicioLayeredPane
														,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE)
														
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														1000,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(134, Short.MAX_VALUE)));
		

		cargarTabla(new ServicioDVO()) ;
		pack();
	}
	
	
	public static MantenedorServiciosForm getInstance() throws PropertyVetoException {
		if (myInstance == null) {
			myInstance = new MantenedorServiciosForm();
		}
		return myInstance;
	}
	
	
	
	
	
	
	
	private void cargarTabla(ServicioDVO servicioDVO) 
	{
		

		ServicioBO[] servicio;
		try {
			servicio = PeluFacade.getPeluqueriaComponent().getServicios(servicioDVO);
			if(servicio != null)
			{
				DefaultTableModel tableModel = (DefaultTableModel) this.serviciosTable.getModel();
				for (ServicioBO servicioBO : servicio) 
				{
					if(servicioBO != null){
						tableModel.addRow(new Object[] { 
								servicioBO.getId(),
								servicioBO.getNombre(),
								servicioBO.getPrecio(),
								servicioBO.getTipoCabello(),
								servicioBO.getTieneFormula() == 1 ?"si":"no"
						});
						
					}
				}
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private void guardarServicio(){
		ServicioDVO servicio = new ServicioDVO();
		servicio.setNombre(nombreServicioField.getText());
		servicio.setValor(Integer.parseInt(precioField.getText()));
		servicio.setTipoCabelloId(((TablaDVO)tipoCabelloCombo.getSelectedItem()).getIdentificador());
		
		if(tieneFormulaCheckBox.isSelected()){
			servicio.setTieneFormula(1);
		}else{
			servicio.setTieneFormula(0);
		}
		
		try {
			PeluFacade.getPeluqueriaComponent().insertServicio(servicio);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
		}
		
	
		
	}
	
	
	private void actualizarServicio(){
		ServicioDVO servicio = new ServicioDVO();
		servicio.setId(Integer.parseInt(codigoField.getText()));
		servicio.setNombre(nombreServicioField.getText());
		servicio.setValor(Integer.parseInt(precioField.getText()));
		servicio.setTipoCabelloId(((TablaDVO)tipoCabelloCombo.getSelectedItem()).getIdentificador());
		
		if(tieneFormulaCheckBox.isSelected()){
			servicio.setTieneFormula(1);
		}else{
			servicio.setTieneFormula(0);
		}
		
		try {
			PeluFacade.getPeluqueriaComponent().updateServicio(servicio);
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
		}
		
	}
		
	
	private void eliminarServicio(){
		
		
		try {
			PeluFacade.getPeluqueriaComponent().deleteServicioById(Integer.parseInt(codigoField.getText()));
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), e.toString(), JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void TableMouseClicked(java.awt.event.MouseEvent evt) {

		ServicioBO servicio = new ServicioBO();
		
		servicio.setId(Integer.parseInt( ""+serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 0)));
		servicio.setNombre( ""+serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 1));
		servicio.setPrecio(Integer.parseInt( ""+serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 2)));
		servicio.setTipoCabello((TablaDVO)serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 3));
		

		actualizarButton.setEnabled(true);
		eliminarButton.setEnabled(true);
		guardarButton.setEnabled(false);
				
		codigoField.setText(""+servicio.getId());
		nombreServicioField.setText(servicio.getNombre());
		precioField.setText(""+servicio.getPrecio());
		if(servicio.getTipoCabello()!= null && servicio.getTipoCabello().getIdentificador() != 0){
			tipoCabelloCombo.setSelectedIndex(((TablaDVO)serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 3)).getIdentificador());
		}
		
		;
		
		if(serviciosTable.getValueAt(serviciosTable.getSelectedRow(), 4).equals("si")){
			tieneFormulaCheckBox.setSelected(true);
		}else{
			tieneFormulaCheckBox.setSelected(false);
		}
		
		
		
		
		
		
	}

	
	
	void limpiarTabla (){
		
		DefaultTableModel tableModel = (DefaultTableModel) this.serviciosTable.getModel();
		
		int a = tableModel.getRowCount();
		
		for (int i = 0; i < a; i++) {
			tableModel.removeRow(0);
			
		}
		
	}
	
	
	public void limpiarFormulario(){
		actualizarButton.setEnabled(false);
		eliminarButton.setEnabled(false);
		guardarButton.setEnabled(true);
		codigoField.setText("");
		nombreServicioField.setText("");
		precioField.setText("");
		tipoCabelloCombo.setSelectedIndex(0);
		tieneFormulaCheckBox.setSelected(false);
		
		limpiarTabla();
		
	}

}